/**
 * Copyright (c) 2007,中企动力华南研发中心<br>
 * All rights reserved.<br>
 */
package com.ce.pms.charge.web;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.ce.pms.base.web.PmsBaseAction;

import com.ce.pms.charge.service.IChargeItemService;
import com.ce.pms.charge.service.IPrePaySchService;
import com.ce.pms.charge.vo.ChargeItemVO;
import com.ce.pms.charge.vo.PrePaySchSearchVO;
import com.ce.pms.charge.vo.PrePaySchVO;
import com.ce.pms.charge.vo.SchDetailVO;
import com.sitechasia.webx.components.xtable.util.ExtremeUtils;
import com.sitechasia.webx.core.annotation.Read;
import com.sitechasia.webx.core.support.Page;

/**
 * 功能描述：预交款方案管理ACTION<br>
 * @author 易贤建 创建日期：2008-8-6<br>
 */
public class PreparePaySchemeAction extends PmsBaseAction {

	private IPrePaySchService prePaySchService = null;
	//收费项目
	private IChargeItemService chargeItemService = null;
	/**
	 * 功能描述: 初始化预交款方案主页面 <br>
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-8-6  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public String initMain(){
		return "main";
	}

	/**
	 * 功能描述:  初始化预交款方案列表页面<br>
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-8-6  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public String initList(){
		setAttribute("totalRows", Integer.valueOf(0) );
		return "list";
	}
	/**
	 * 功能描述:  初始化添加页面<br>
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-8-7  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public String initAdd(){
		//处理所有未选择收费项目
		setUnSelectCharegeItem(null);
		return "add";
	}

	/**
	 * 功能描述:  <br>
	 * @param prePaySchId
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-8-12  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public String initEdit(@Read(key="prePaySchId")Long prePaySchId){
		//缓存预交方案VO
		PrePaySchVO prePaySchVO = prePaySchService.findPrePaySchById(prePaySchId);
		setAttribute("prePaySchVO", prePaySchVO);
		//缓存未选择列表
		String itemIds = getCharegeItemIdsFormPrePaySch(prePaySchVO); //获取已选择收费项目ID字符串
		List<ChargeItemVO> chargeItems = chargeItemService.queryChargeItemList(itemIds, true);
		setAttribute("chargeItems", chargeItems);
		return "edit";
	}

	public String initDetail(@Read(key="prePaySchId")Long prePaySchId){
		//缓存预交方案VO
		PrePaySchVO prePaySchVO = prePaySchService.findPrePaySchById(prePaySchId);
		setAttribute("prePaySchVO", prePaySchVO);
		return "detail";
	}
	/**
	 * 功能描述: 查询列表 <br>
	 * @param schemeName
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-8-12  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public String list(@Read(key="schemeName")String schemeName) throws Exception{
		schemeName = schemeName == null? null : (String)java.net.URLDecoder.decode(schemeName.trim(),"UTF-8");
		//组装查询对象
		PrePaySchSearchVO preSearch = new PrePaySchSearchVO();
		preSearch.setPageSize(ExtremeUtils.getPageSize( getRequest() ));
		preSearch.setPageNo( ExtremeUtils.getPageNo( getRequest() ) );
		String sortString = ExtremeUtils.getOrderString(getRequest());
		if(sortString != null && !"".equals(sortString))
			preSearch.setSortStr(sortString.replace(' ', ','));
		preSearch.setSchemeName(schemeName);
		//设置查询结果
		Page page = prePaySchService.queryPrePaySchListByPage(preSearch);
		setAttribute("totalRows", Integer.valueOf( String.valueOf( page.getTotalCount() ) ) );
		setAttribute("prePaySchs", page.getResult());
		return "list";
	}
	/**
	 * 功能描述:  添加预交方案<br>
	 * @param prePaySchVO
	 * Create author:yixianjian <br>
	 * Create on:2008-8-12  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public String add(@Read PrePaySchVO prePaySchVO){
		prePaySchVO.setCreator(getUserId());
		processSchDetail(prePaySchVO); //处理所选择的收费项目列表
		prePaySchService.addPrePaySch(prePaySchVO);
		return null;
	}

	/**
	 * 功能描述: 修改预交方案<br>
	 * @param prePaySchVO
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-8-12  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public String edit(@Read PrePaySchVO prePaySchVO){
		prePaySchVO.setModifier(getUserId());
		processSchDetail(prePaySchVO); //处理所选择的收费项目列表
		prePaySchService.editPrePaySch(prePaySchVO);
		return null;
	}
	/**
	 * 功能描述: 根据ID删除预交方案 <br>
	 * @param prePaySchId    将被删除的预交方案ID
	 * Create author:yixianjian <br>
	 * Create on:2008-8-12  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public void delete(@Read(key="prePaySchId")Long prePaySchId){
		prePaySchService.deletePrePaySch(prePaySchId);
	}
	/**
	 * 功能描述:  验证预交方案名称唯一<br>
	 * Create author:yixianjian <br>
	 * Create on:2008-8-12  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public void validateSchemeName(@Read(key="prePaySchId")Long prePaySchId,
								   @Read(key="schemeName")String schName){
		boolean flag = false;
		if(prePaySchId == null)
			flag = prePaySchService.checkIsExistPrePaySch(schName);
		else
			flag = prePaySchService.checkIsExistPrePaySch(prePaySchId, schName);
		renderText(flag?"false":"true");
	}
	/**
	 * 功能描述: 处理预交方案明细 <br>
	 * 从JSP页面传过来的字符串包括收费项目ID,预交序号.其拼装方式为:"收费项目1ID,预交序号1-收费项目2ID,预交序号2-"
	 * @param prePaySchVO
	 * Create author:yixianjian <br>
	 * Create on:2008-8-12  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private void processSchDetail(PrePaySchVO prePaySchVO) {
		if(prePaySchVO == null) return;

		List<SchDetailVO> schDetailList = new ArrayList<SchDetailVO>();
		String strSchDetail = getRequest().getParameter("schDetail");
		if(strSchDetail != null){
			String[] schDetails = strSchDetail.split("-");
			for(int i = 0; i < schDetails.length; i++){
				String[] schDetail = schDetails[i].split(",");
				SchDetailVO schDetailVO = new SchDetailVO();
				schDetailVO.setChargeItemId(Long.valueOf(schDetail[0]));
				schDetailVO.setSeqence(Short.valueOf(schDetail[1]));
				schDetailList.add(schDetailVO);
			}
		}
		prePaySchVO.setSchDetails(schDetailList);
	}
	private String getCharegeItemIdsFormPrePaySch(PrePaySchVO prePaySchVO) {
		List<SchDetailVO> prePaySchList = prePaySchVO.getSchDetails();
		StringBuffer ids = new StringBuffer("");
		if(prePaySchList != null)
			for(Iterator<SchDetailVO> iterator = prePaySchList.iterator();iterator.hasNext();){
				ids.append(iterator.next().getChargeItemId());
				ids.append(",");
			}
		return ids.substring(0, ids.lastIndexOf(","));
	}
	/**
	 * 功能描述:  缓存所有未选择、可抵充收费项目<br>
	 * Create author:yixianjian <br>
	 * Create on:2008-8-7  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private void setUnSelectCharegeItem(String ids){
		List list = chargeItemService.queryChargeItemList(ids, false);
		setAttribute("chargeItems", list);
	}
	public void setPrePaySchService(IPrePaySchService prePaySchService) {
		this.prePaySchService = prePaySchService;
	}

	public void setChargeItemService(IChargeItemService chargeItemService) {
		this.chargeItemService = chargeItemService;
	}


}
